package com.gfxy.modules.items.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gfxy.modules.items.entity.Sxjd;
import com.gfxy.modules.items.entity.teachingresult.TeachingResultContent;
import com.gfxy.modules.items.mapper.SxjdMapper;
import com.gfxy.modules.items.service.ISxjdService;
import com.gfxy.modules.items.vo.SxjdMemberPage;
import com.gfxy.modules.excel.ExcelUtil;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 实训基地
 * @Author: jeecg-boot
 * @Date:   2021-08-23
 * @Version: V1.0
 */
@Service
public class SxjdServiceImpl extends AbstractProjectServiceImpl<SxjdMapper, Sxjd> implements ISxjdService {


	protected int memberCellNum = 4;

	@Override
	protected void pass(String id, String isPass) {

	}

	@Override
	protected String[] getMemberArray(Row row) {
		String nameStrs = ExcelUtil.formatCell(row.getCell(memberCellNum));
		return nameStrs.split(memberNameSeparator);
	}

	@Override
	protected Sxjd getOneProject(Row row) {

		String date = ExcelUtil.formatCell(row.getCell(0));

		String projectName = ExcelUtil.formatCell(row.getCell(1));
		String amount = ExcelUtil.formatCell(row.getCell(2));
		String stepName = ExcelUtil.formatCell(row.getCell(3));

		Sxjd sxjd = new Sxjd();

		sxjd.setDate(date);
		sxjd.setProjectName(projectName);
		sxjd.setAmount(Double.parseDouble(amount));
		sxjd.setStep(translateStep(stepName));
		sxjd.setIsPass(0);
		//这个地方不要忘了-算总分
		sxjd.setTotalScore(this.getTotalScore(sxjd));
		return sxjd;
	}


	@Override
	public Double getTotalScore(Sxjd sxjd) {

		double result = 0.0;
		Double amount = sxjd.getAmount();
		int step = sxjd.getStep();

		if (amount<50){
			result=30;
		}else if (amount>=50&&amount<100){
			result=40;
		}else if (amount>=100&&amount<200){
			result=50;
		}else if (amount>=200&&amount<300){
			result=60;
		}else if (amount>=300&&amount<400){
			result=70;
		}else if (amount>=400&&amount<500){
			result=80;
		}else if (amount>=500&&amount<1000){
			result=100;
		}else if (amount>=1000){
			result=120;
		}

		if (step==1 || step ==2){
			result *= 0.15;
		}else if (step == 3 || step == 4) {
			result *= 0.2;
		}else if (step == 5 || step == 6 || step ==7) {
			result *= 0.1;
		}


		return result;

//		BigDecimal value1 = new BigDecimal( result );
//		BigDecimal value2 = new BigDecimal( 3.0);
//		double result2 = value1.divide( value2, 0, BigDecimal.ROUND_HALF_UP ).doubleValue();
//		if(step == 2){
//			result = result - result2;
//		} else if(step ==1){
//			result = result2;
//		} else {
//			result = 0;
//		}
	}


	private Integer translateStep(String stepName) {
		if(stepName == null || stepName.equals("")){
			return 0;
		} else if(stepName.equals("调研")){
			return 1;
		} else if(stepName.equals("申报")){
			return 2;
		} else if(stepName.equals("论证")){
			return 3;
		}else if(stepName.equals("建设")){
			return 4;
		}else if(stepName.equals("验收")){
			return 5;
		}else if(stepName.equals("报账")){
			return 6;
		}else if(stepName.equals("资料报送整理")){
			return 7;
		}else {
			return 0;
		}
	}


	@Override
	public IPage<SxjdMemberPage> getSxjdMemberPageListByYear(String date, Integer pageNo, Integer pageSize) {
		Page<SxjdMemberPage> page = new Page<>(pageNo,pageSize);
		return page.setRecords(this.baseMapper.selectSxjdMemberPageListByYear(date));
	}

	@Override
	public List<SxjdMemberPage> getSxjdMemberPageListByYear(String date) {
		return this.baseMapper.selectSxjdMemberPageListByYear(date);
	}


//	@Override
//	public Double getTotalScore(Sxjd sxjd) {
//		Double amout = sxjd.getAmount();
//		double result = 0.0;
//		if (amout<50){
//			result=30;
//		}else if (amout>=50&&amout<100){
//			result=40;
//		}else if (amout>=100&&amout<200){
//			result=50;
//		}else if (amout>=200&&amout<300){
//			result=60;
//		}else if (amout>=300&&amout<400){
//			result=70;
//		}else if (amout>=400&&amout<500){
//			result=80;
//		}else if (amout>=500&&amout<1000){
//			result=100;
//		}else if (amout>=1000){
//			result=120;
//		}
//		return result;
//	}

	@Override
	protected List<TeachingResultContent> createTeachingResultContent(Sxjd jxcg)  {

		List<TeachingResultContent> result = new ArrayList<>();

		// 类型
		TeachingResultContent classTeachingResultContent = new TeachingResultContent();
		classTeachingResultContent.setType(1);
		classTeachingResultContent.setLevel(jxcg.getStep());

		try {
			classTeachingResultContent.setValue(dictTranslater.getDictValue(SxjdMemberPage.class, "step", String.valueOf(jxcg.getStep())));
			result.add(classTeachingResultContent);
		} catch (NoSuchFieldException e) {
			log.error(e.getMessage(),e);
		}

		return result;
	}

}
